<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="片区名称">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入片区名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="serachFn"
        />
      </el-form-item>
      <el-form-item label="片区编码">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入片区编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="serachFn"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="serachFn"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dict:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="areaName" label="片区名称"> </el-table-column>
      <el-table-column prop="areaCode" label="片区编码"> </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dict:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="handleQuery"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="片区名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入片区名称" />
        </el-form-item>
        <el-form-item label="片区编号" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入片区编号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAreaApi, areaListApi, putAreaApi ,delAreaApi} from "@/api/party/index";
export default {
  data() {
    return {
      title: "",
      open: false,
      loading: false,
      list: [],
      queryParams: {
        areaName: "",
        areaCode: "",
        params: { pageNum: 1, pageSize: 10 },
      },
      total: 0,
      form: {
        areaCode: "",
        areaName: "",
      },
      rules: {
        areaCode: [
          { required: true, message: "片区编号不能为空", trigger: "blur" },
        ],
        areaName: [
          { required: true, message: "片区名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.handleQuery();
  },
  methods: {
     handleDelete(row) {
      this.$modal.confirm('是否确认删除"' + row.areaName + '"的数据项？').then(function() {
        return delAreaApi(row.areaId);
      }).then(() => {
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleUpdate(row) {
      this.form = {
        areaCode: "",
        areaName: "",
      };
      this.title = "修改片区";
      this.open = true;
       this.form = JSON.parse(JSON.stringify(row));
    },
    handleQuery() {
      this.loading = true;
      areaListApi(this.queryParams).then((response) => {
        this.total = response.total;
        this.list = response.rows;
        this.loading = false;
      });
    },
    serachFn() {
      this.queryParams.params.pageNum = 1;
      this.handleQuery();
    },
    resetQuery() {
      this.queryParams = {
        areaName: "",
        areaCode: "",
        params: { pageNum: 1, pageSize: 10 },
      };
      this.handleQuery();
    },
    handleAdd() {
      this.form = {
        areaCode: "",
        areaName: "",
      };
      this.open = true;
      this.title = "新增片区";
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.areaId) {
            putAreaApi(this.form).then((res) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.handleQuery();
            });
          } else {
            addAreaApi(this.form).then((res) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.handleQuery();
            });
          }
        }
      });
    },
    cancel() {
      this.resetForm("queryParams");
      this.open = false;
    },
  },
};
</script>

<style>
</style>